package com.epalmpay.service.yunstream.impl;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.agent.AgentInfoOutDTO;
import com.epalmpay.dto.agent.AgentListOutDTO;
import com.epalmpay.dto.yunliu.profit.ObdProfitRuleListOutDTO;
import com.epalmpay.dto.yunliu.profit.ObdProfitRuleOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.YsObdProfitRule;
import com.epalmpay.entity.YsOpenCreditProfitRule;
import com.epalmpay.entity.YsPolicyProfitRule;
import com.epalmpay.enumdef.YunStreamEnum;
import com.epalmpay.mapper.AgentMapper;
import com.epalmpay.mapper.YsObdProfitRuleMapper;
import com.epalmpay.mapper.YsOpenCreditProfitRuleMapper;
import com.epalmpay.mapper.YsPolicyProfitRuleMapper;
import com.epalmpay.service.yunstream.IObdProfitRule;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("ObdProfitRuleService")
public class ObdProfitRuleImpl implements IObdProfitRule{

    @Resource
    private YsObdProfitRuleMapper ysObdProfitRuleMapper;

    @Resource
    private AgentMapper agentMapper;

    @Resource
    private YsOpenCreditProfitRuleMapper openCreditCardProfitRuleMapper;

    @Resource
    private YsPolicyProfitRuleMapper policyProfitRuleMapper;


    @Override
    public ObdProfitRuleListOutDTO ysObdProfitRuleList(YsObdProfitRule YsObdProfitRule) throws ApplicationException {
        ObdProfitRuleListOutDTO ObdProfitRuleListOutDTO =new ObdProfitRuleListOutDTO();
        try{
            List<YsObdProfitRule> list=ysObdProfitRuleMapper.queryYsObdProfitRuleList(YsObdProfitRule);
            ObdProfitRuleListOutDTO.setYsObdProfitRuleList(list);
            return ObdProfitRuleListOutDTO;
        }catch (Exception e) {
            ObdProfitRuleListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }

        return ObdProfitRuleListOutDTO;

    }

    @Override
    public ObdProfitRuleOutDTO DeleteObdProfitRule(YsObdProfitRule ysObdProfitRule) throws ApplicationException {
        ObdProfitRuleOutDTO ObdProfitRuleOutDTO =new ObdProfitRuleOutDTO();
        int status=ysObdProfitRuleMapper.updateByPrimaryKeySelective(ysObdProfitRule);


        if (status>0)
        {
            ObdProfitRuleOutDTO.setErrorCode(Rescode.SUCCESS, "删除成功");
        }
        return ObdProfitRuleOutDTO;
    }


    @Override
    public ObdProfitRuleOutDTO SetysObdProfitRule(YsObdProfitRule ysObdProfitRule) throws ApplicationException {
        ObdProfitRuleOutDTO ObdProfitRuleOutDTO =new ObdProfitRuleOutDTO();
        if (Tools.isEmpty(ysObdProfitRule.getId()+"")){
            ysObdProfitRule.setGmtCreate(new Date());
            ysObdProfitRule.setGmtModify(new Date());
            if (!Tools.isEmpty(ysObdProfitRule.getAgentId()+"")){
                YsObdProfitRule ysObdProfitRule1=ysObdProfitRuleMapper.selectByAgentId(ysObdProfitRule);

               if(ysObdProfitRule1!=null){
                   ysObdProfitRule.setId(ysObdProfitRule1.getId());
                   if ((ysObdProfitRule.getFlag()!=1)&&(ysObdProfitRule1.getReverseField1()!=0)){
                       if (!Tools.isEmpty(ysObdProfitRule1.getProfit1()+"")){
                           ysObdProfitRule.setProfit1(ysObdProfitRule1.getProfit1());
                       }
                       if (!Tools.isEmpty(ysObdProfitRule1.getProfit2()+"")){
                           ysObdProfitRule.setProfit1(ysObdProfitRule1.getProfit2());
                       }
                       if (!Tools.isEmpty(ysObdProfitRule1.getProfit3()+"")){
                           ysObdProfitRule.setProfit1(ysObdProfitRule1.getProfit3());
                       }
                       if (!Tools.isEmpty(ysObdProfitRule1.getTotalProfit()+"")){
                           ysObdProfitRule.setTotalProfit(ysObdProfitRule1.getTotalProfit());
                       }
                   }else{
                       YsObdProfitRule ysObdProfitRule2=ysObdProfitRuleMapper.selectCommonOBDProfitRule(ysObdProfitRule.getGroupId());

                       if (ysObdProfitRule2!=null){
                           ysObdProfitRule.setProfit1(ysObdProfitRule2.getProfit1());
                           ysObdProfitRule.setProfit2(ysObdProfitRule2.getProfit2());
                           ysObdProfitRule.setProfit3(ysObdProfitRule2.getProfit3());
                           ysObdProfitRule.setTotalProfit(ysObdProfitRule2.getTotalProfit());
                       }
                   }

                   int status=ysObdProfitRuleMapper.updateByPrimaryKeySelective(ysObdProfitRule);
                   if (status>0)
                   {
                       ObdProfitRuleOutDTO.setErrorCode(Rescode.SUCCESS, "设置成功");
                   }
                   return ObdProfitRuleOutDTO;


               }else{
                   if (ysObdProfitRule.getFlag()!=1){
                       YsObdProfitRule ysObdProfitRule2=ysObdProfitRuleMapper.selectCommonOBDProfitRule(ysObdProfitRule.getGroupId());

                       if (ysObdProfitRule2!=null){
                           ysObdProfitRule.setProfit1(ysObdProfitRule2.getProfit1());
                           ysObdProfitRule.setProfit2(ysObdProfitRule2.getProfit2());
                           ysObdProfitRule.setProfit3(ysObdProfitRule2.getProfit3());
                           ysObdProfitRule.setTotalProfit(ysObdProfitRule2.getTotalProfit());
                       }
                   }

                   int status=ysObdProfitRuleMapper.insert(ysObdProfitRule);
                   if (status>0)
                   {
                       ObdProfitRuleOutDTO.setErrorCode(Rescode.SUCCESS, "设置成功");
                   }
                   return ObdProfitRuleOutDTO;
               }

            }else{
                if (ysObdProfitRule.getFlag()!=1){
                    YsObdProfitRule ysObdProfitRule3=ysObdProfitRuleMapper.selectCommonOBDProfitRule(ysObdProfitRule.getGroupId());

                    if (ysObdProfitRule3!=null){
                        ysObdProfitRule.setProfit1(ysObdProfitRule3.getProfit1());
                        ysObdProfitRule.setProfit2(ysObdProfitRule3.getProfit2());
                        ysObdProfitRule.setProfit3(ysObdProfitRule3.getProfit3());
                        ysObdProfitRule.setTotalProfit(ysObdProfitRule3.getTotalProfit());
                    }
                }

                int status=ysObdProfitRuleMapper.insert(ysObdProfitRule);
                if (status>0)
                {
                    ObdProfitRuleOutDTO.setErrorCode(Rescode.SUCCESS, "设置成功");
                }
                return ObdProfitRuleOutDTO;
            }


        }else{
            ysObdProfitRule.setGmtModify(new Date());
            int status=ysObdProfitRuleMapper.updateByPrimaryKeySelective(ysObdProfitRule);
            if (status>0)
            {
                ObdProfitRuleOutDTO.setErrorCode(Rescode.SUCCESS, "设置成功");
            }
            return ObdProfitRuleOutDTO;
        }

    }


    @Override
    public AgentListOutDTO agentList(Long groupId, Page page,Integer otherType)  {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", groupId);
        param.put("agentLevel", "1");

        int sumCount = agentMapper.findAgentListWithPageCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return null;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));


        ArrayList<AgentInfoOutDTO> list = agentMapper.findAgentListWithPage(param);
        AgentListOutDTO agentListOutDTO = new AgentListOutDTO();
        agentListOutDTO.setAgentOutDTOList(list);
        agentListOutDTO.setPage(page);

        if (otherType== YunStreamEnum.ProfitType.obdProfit.getType()){
            List<YsObdProfitRule> tempList =ysObdProfitRuleMapper.queryObdAgentList(groupId);
            agentListOutDTO.setObdAgentList(tempList);
        }else if(otherType==YunStreamEnum.ProfitType.policyProfit.getType()){
            List<YsPolicyProfitRule> tempList =policyProfitRuleMapper.queryPolicyAgentList(groupId);
            agentListOutDTO.setPolicyAgentList(tempList);
        }else if(otherType==YunStreamEnum.ProfitType.creditCardOpenCardProfit.getType()){
            List<YsOpenCreditProfitRule> tempList =openCreditCardProfitRuleMapper.queryCreditCardOpenCardAgentList(groupId);
            agentListOutDTO.setCreditCardOpenCardAgentList(tempList);
        }
        return agentListOutDTO;
    }
}
